package cn.xorange.commons.security.configuration;

import cn.xorange.commons.security.constants.SecurityConstants;
import cn.xorange.commons.security.filter.JwtAuthTokenFilter;
import cn.xorange.commons.security.filter.LoginFilterService;
import cn.xorange.commons.security.filter.LoginFilter;
import cn.xorange.commons.security.provider.CustomAuthProvider;
import cn.xorange.commons.security.service.LoginHandlerFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : yangjian
 * @date : 2023/11/30
 */
@Component
public class LoginFilterConfig extends AbstractHttpConfigurer<LoginFilterConfig, HttpSecurity> {

    @Resource
    CustomAuthProvider customAuthProvider;
    @Resource
    LoginHandlerFactory loginHandlerFactory;

    @Resource
    LoginFilterService loginFilterService;

    @Override
    public void configure(HttpSecurity builder){
        LoginFilter filter = new LoginFilter(SecurityConstants.URL_LOGIN, loginFilterService, loginHandlerFactory);
        filter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        //webLoginFilter.setRememberMeServices(new TokenBasedRememberMeServices(config.getRememberMeKey(),));

        //将设置完毕的 Filter 与 Provider 添加到配置中，将自定义的登录Filter 加到 UsernamePasswordAuthenticationFilter 之前
        builder.authenticationProvider(customAuthProvider).addFilterBefore(filter, LogoutFilter.class);

    }


}
